#include "global.h"

void sleep(int event)
{
	printf("  * P%d going to sleep\n", running->processID);
	running->event = event;
	running->status = SLEEP;
	enqueue(running, &sleepQueue);
	tswitch();
}

void wakeup(int event)
{
	int i;
	Process* curProc = sleepQueue.top;
	Process* prvProc = 0, *tmp = 0;
	
	for (i = 0; ((i < sleepQueue.size) && (curProc != 0)); ++i)
	{
		if (curProc->event == event)
		{
			if (!prvProc)
			{
				sleepQueue.top = curProc->next;
			}
			else
			{
				prvProc->next = curProc->next;
			}
			curProc->status = READY;
			tmp = curProc->next;
			enqueue(curProc, &readyQueue);
		}
		else
		{
			prvProc = curProc;
			tmp = curProc->next;
		}
		curProc = tmp;
	}
}
